﻿using System.Application;
using System.Collections.Generic;
using System.Domain.Entity;
using System.Domain.IRepository;
using System.Linq;
using System.Repository;
using Microsoft.AspNetCore.Mvc;
using Ubiquity.Core.Web.TreeView;
using Ubiquity.Web.Filters;


namespace Ubiquity.Web.Areas.SystemManage.Controllers
{
    /// <summary>
    /// 角色授权表 控制器 
    /// </summary>
    [Area("SystemManage")]
    public class SysRoleAuthorizeController : BaseController
    {
        

        private readonly SysRoleAuthorizeService _roleAuthorizeService;
        private readonly SysModuleService _moduleService;
        private readonly SysModuleAuthorizeService _moduleButtonService;


        public SysRoleAuthorizeController(SysRoleAuthorizeService roleAuthorizeService, SysModuleService moduleService, SysModuleAuthorizeService moduleButtonService)
        {
            _roleAuthorizeService = roleAuthorizeService;
            _moduleService = moduleService;
            _moduleButtonService = moduleButtonService;
        }

        public IActionResult GetPermissionTree(string roleId)
        {
            var moduledata = _moduleService.GetList().OrderBy(t => t.SortCode).ToList();
            var buttondata = _moduleButtonService.GetList();
            var authorizedata = new List<SysRoleAuthorizeEntity>();
            if (!string.IsNullOrEmpty(roleId))
            {
                authorizedata = _roleAuthorizeService.GetList(roleId);
            }
            var treeList = new List<TreeViewModel>();
            foreach (SysModuleEntity item in moduledata)
            {
                TreeViewModel tree = new TreeViewModel();
                tree.Id = item.Id;
                tree.Text = item.FullName;
                tree.Value = item.EnCode;
                tree.ParentId = item.ParentId;
                tree.Isexpand = true;
                tree.Complete = true;
                tree.Showcheck = true;
                tree.Checkstate = authorizedata.Count(t => t.ModuleId == item.Id);
                // tree.Checkstate = 0;
                tree.HasChildren = true;
                tree.Img = item.Icon == "" ? "" : item.Icon;
                treeList.Add(tree);
            }
            foreach (SysModuleAuthorizeEntity item in buttondata)
            {
                TreeViewModel tree = new TreeViewModel();
                bool hasChildren = buttondata.Count(t => t.ParentId == item.Id) != 0;
                tree.Id = item.Id;
                tree.Text = item.FullName;
                tree.Value = item.EnCode;
                tree.ParentId = item.ParentId == "0" ? item.ModuleId : item.ParentId;
                tree.Isexpand = true;
                tree.Complete = true;
                tree.Showcheck = true;
                tree.Checkstate = authorizedata.Count(t => t.AuthorizeId == item.Id);
                //tree.Checkstate = 0;
                tree.HasChildren = hasChildren;
                tree.Img = item.Icon == "" ? "" : item.Icon;
                treeList.Add(tree);
            }
            return Content(treeList.TreeViewJson());
        }

    }
}
